package com.collectplus.express.home;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.collectplus.express.R;
import com.collectplus.express.logic.AppHttp;
import com.collectplus.express.logic.AppLogic;
import com.collectplus.express.model.BannerBean;
import com.shouhuobao.bhi.stamp.StampStoreActivity;

import java.util.ArrayList;

import droid.frame.activity.base.BaseAdapterExt;

/**
 * 首页banner轮播图item
 *
 * @author coffee <br>
 *         2016-10-10下午3:30:19
 */
public class BannerImageAdapter extends BaseAdapterExt<BannerBean> {
    // private String[] imageUrls = { "http://img.guoer.shouhuobao.com/parcel/banner%EF%BC%8Dandroid.png", //
    // "http://img.guoer.shouhuobao.com/parcel/banner2%EF%BC%8Dandroid.png" };

    public BannerImageAdapter(ArrayList<BannerBean> items, Activity context) {
        super(items, context);
    }

    @Override
    public int getCount() {
        // return imageUrls.length;
        if (super.getCount() == 0) {
            ArrayList<BannerBean> items = new ArrayList<BannerBean>();
            if (context instanceof StampStoreActivity){
                items.add(new BannerBean(R.drawable.stamp_store_banner));
            }else {
                items.add(new BannerBean(R.drawable.home_banner));
            }
            setItems(items, false);
        }
        return super.getCount();
    }

    @Override
    public BannerBean getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.home_banner_item, parent, false);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.home_banner_image);
            convertView.setTag(holder);
        } else {
            convertView.getTag();
        }
        final BannerBean item = getItem(position);
        if (item.getImgRes() != 0) {
            holder.img.setImageResource(item.getImgRes());
        } else {
            AppHttp.getInstance().loadImage(item.getImgUrl(), holder.img, true);
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AppLogic.disposeBannerData(context, item);
                }
            });
        }
        return convertView;
    }

    private class ViewHolder {
        ImageView img;
    }
}
